//使用机构活动模板
exports.editBulkPurchaseActivity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var couponType="";

    var couponId="";

    var participantcouponType="";

    var participantcouponId="";

    //任务流创建
    async.waterfall([
                        //机构模板活动
                        function (cb) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url:CORE_APIConfig.coreAPI.searchOrganizationActivity+"/oa_id/"+restfulParams.oa_id+"/oa_type/0" ,
                                    method: 'GET',
                                },
                                function (err,data) {
                                    if(err){
                                        return RES.response(err, false, data);
                                    }
                                    if(data.data == '' ){
                                        return RES.response(res, false, data);
                                    }
                                    resultData.activity = data.data;
                                    cb();
                                }
                            )
                        },
                        //机构模板活动拓展
                        function (cb) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityConfig+"/bpac_oaid/"+restfulParams.oa_id ,
                                    method: 'GET',

                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(err, false, data);
                                    }
                                    if(data.data == '' ){
                                        return RES.response(res, false, data);
                                    }
                                    resultData.config = data.data;
                                    if(resultData.config[0].bpac_builder_coupon_id == 0 && resultData.config[0].bpac_participant_coupon_id == 0){
                                        cb();
                                    }
                                    couponType = resultData.config[0].bpac_builder_coupon_type;
                                    couponId = resultData.config[0].bpac_builder_coupon_id;
                                    participantcouponType = resultData.config[0].bpac_participant_coupon_type;
                                    participantcouponId = resultData.config[0].bpac_participant_coupon_id;
                                    cb();
                                }
                            );
                        },
                        
                        //查询音乐信息
                        function(cb){
                            R.SEND_HTTP(
                                req,
                                {
                                    url:CORE_APIConfig.coreAPI.searchActivityMusic+"/am_id/"+resultData.activity[0].oa_music_id,
                                    method: 'GET',
                                },
                                function (err,data) {
                                    resultData.musicData = data.data;
                                    cb();
                                }
                            )
                        },

                        //查询皮肤信息
                        function(cb){
                            R.SEND_HTTP(
                                req,
                                {
                                    url:CORE_APIConfig.coreAPI.searchActivitySkin+"/as_id/"+resultData.activity[0].oa_skin_id,
                                    method: 'GET',
                                },
                                function (err,data) {
                                    resultData.skinData = data.data;
                                    cb();
                                }
                            )
                        },

                        //机构模板活动内容
                        function (cb) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url:CORE_APIConfig.coreAPI.searchBulkPurchaseActivityContent+"/bpac_oaid/"+restfulParams.oa_id+"/sort_order/bpac_order-asc" ,
                                    method: 'POST',
                                    data : {
                                        fields:[
                                            "bpac_type as type",
                                            "bpac_content as content"
                                        ].join(",")
                                    }
                                },
                                function (err,data) {
                                    if(err){
                                        return cb(err,data);
                                    }
                                    resultData.content = data.data;
                                    cb();
                                }
                            )
                        }

                        //开团优惠券
                        /*
                        function(cb){
                            if(couponType == 0 && couponId != 0){
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+couponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        if(data.data !='') {
                                            resultData.config[0].couponName = data.data[0].cc_name;
                                            resultData.config[0].couponStartTime = data.data[0].cc_start_time;
                                            resultData.config[0].couponEndTime = data.data[0].cc_end_time;
                                            cb();
                                        }else {
                                            cb();
                                        }
                                    }
                                );
                            }else if(couponType == 1 && couponId != 0){

                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+couponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        if(data.data !='') {
                                            //console.log("-----------");
                                            resultData.config[0].couponName = data.data[0].cc_name;
                                            resultData.config[0].couponStartTime = data.data[0].cc_start_time;
                                            resultData.config[0].couponEndTime = data.data[0].cc_end_time;
                                            cb();
                                        }else {
                                            //console.log("-----------");
                                            cb();
                                        }
                                    }
                                );
                            }else if(couponId != 0){
                                console.log(123456789);
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.searchExchangeCoupon+"/ec_id/"+couponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        console.log(data.data);
                                        if(data.data !='') {
                                            resultData.config[0].couponName = data.data[0].ec_name;
                                            resultData.config[0].couponStartTime = data.data[0].ec_start_time;
                                            resultData.config[0].couponEndTime = data.data[0].ec_end_time;
                                            cb();
                                        }else {
                                            cb();
                                        }
                                    }
                                );
                            }else {
                                cb();
                            }
                        },
                        */
                        //拼团优惠券
                        /*
                        function (cb) {
                            if(participantcouponType == 0 && participantcouponId != 0){
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+participantcouponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        if(data.data !=''){
                                            resultData.config[0].participantcouponName      = data.data[0].cc_name;
                                            resultData.config[0].participantcouponStartTime = data.data[0].cc_start_time;
                                            resultData.config[0].participantcouponEndTime   = data.data[0].cc_end_time;
                                            cb();
                                        }else {
                                            cb();
                                        }
                                    }
                                );
                            }else if(participantcouponType == 1 && participantcouponId != 0){
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+participantcouponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //console.log(data);
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        if(data.data !='') {
                                            resultData.config[0].participantcouponName = data.data[0].cc_name;
                                            resultData.config[0].participantcouponStartTime = data.data[0].cc_start_time;
                                            resultData.config[0].participantcouponEndTime = data.data[0].cc_end_time;
                                            cb();
                                        }else {
                                            cb();
                                        }
                                    }
                                );
                            }else if(participantcouponId != 0){
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.searchExchangeCoupon+"/ec_id/"+participantcouponId ,
                                        method: 'GET',

                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return cb(err, data);
                                        }
                                        if(data.data !='') {
                                            resultData.config[0].participantcouponName = data.data[0].ec_name;
                                            resultData.config[0].participantcouponStartTime = data.data[0].ec_start_time;
                                            resultData.config[0].participantcouponEndTime = data.data[0].ec_end_time;
                                            cb();
                                        }else {
                                            cb();
                                        }
                                    }
                                );
                            }else {
                                cb();
                            }

                        },
                        //机构模板活动内容
                        function (cb) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url:CORE_APIConfig.coreAPI.searchBulkPurchaseActivityContent+"/bpac_oaid/"+restfulParams.oa_id+"/sort_order/bpac_order-asc" ,
                                    method: 'POST',
                                    data : {
                                        fields:[
                                            "bpac_type as type",
                                            "bpac_content as content"
                                        ].join(",")
                                    }
                                },
                                function (err,data) {
                                    if(err){
                                        return cb(err,data);
                                    }
                                    resultData.content = data.data;
                                    cb();
                                }
                            )
                        },
                        */
                    ], function (err, result) {

                        //返回最终拼接结果
                        RES.response(res, true, resultData);

                    }
    );

}